import Taro from "@tarojs/taro";
import api from '@api/api.js'
import CryptoJS from "crypto-js";
// 图片上传
export const uploadImgs = (file = []) => {
  const includIdFile = file.filter((item) => item.id);
  const uncludIdFile = file.filter((item) => !item.id);
  const singleReqs = uncludIdFile.map((item) => (
    Taro.uploadFile({
      url: api.bseUrl + '/sys/upload/single',
      filePath: item.url,
      name: 'file',
      header: {
        'Authorization': Taro.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
      }
    }))
  )
  return new Promise((resolve, reject) => {
    const result = [];
    Promise.all(singleReqs).then((res) => {
      res.forEach((item) => {
        result.push(JSON.parse(item.data).data.id)
      });
      resolve([...result, ...includIdFile.map(item => item.id)]);
    })
  })
}

// token 解密
export const AES = (res) => {
  const key = CryptoJS.enc.Utf8.parse("1234567887654321");
  const decrypt = CryptoJS.AES.decrypt(res, key, {
    iv: key,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  }).toString(CryptoJS.enc.Utf8);
  return decrypt
}